package com.xmg.p2p.base.service.impl;

import com.xmg.p2p.base.domain.RealAuth;
import com.xmg.p2p.base.domain.Userinfo;
import com.xmg.p2p.base.event.RealAuthSuccessEvent;
import com.xmg.p2p.base.mapper.RealAuthMapper;
import com.xmg.p2p.base.query.PageResult;
import com.xmg.p2p.base.query.RealAuthQueryObject;
import com.xmg.p2p.base.service.IRealAuthService;
import com.xmg.p2p.base.service.IUserinfoService;
import com.xmg.p2p.base.util.BitStatesUtils;
import com.xmg.p2p.base.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class RealAuthServiceImpl implements IRealAuthService {
    @Autowired
    private RealAuthMapper realAuthMapper;
    @Autowired
    private IUserinfoService userinfoService;
    @Autowired
    private ApplicationContext ctx;

    @Override
    public RealAuth findById(Long id) {
        return realAuthMapper.selectByPrimaryKey(id);
    }

    @Override
    public void apply(RealAuth realAuth) {
        Userinfo current = this.userinfoService.getCurrent();
        // 判断当前用户没有实名认证并且当前用户不处于待审核状态
        if (!current.getIsRealAuth() && current.getRealAuthId() == null) {
            // 保存一个实名认证对象;
            realAuth.setState(RealAuth.STATE_NORMAL);
            realAuth.setApplier(UserContext.getLogininfo());
            realAuth.setApplyTime(new Date());
            realAuthMapper.insert(realAuth);
            // 把实名认证的id设置给userinfo
            current.setRealAuthId(realAuth.getId());
            userinfoService.update(current);
        }
    }

    @Override
    public PageResult queryPage(RealAuthQueryObject qo) {
        int i = realAuthMapper.queryForCount(qo);
        if (i > 0) {
            List<RealAuth> query = realAuthMapper.query(qo);
            return new PageResult(query, i, qo.getCurrentPage(), qo.getPageSize());
        }
        return PageResult.empty(qo.getPageSize());
    }

    @Override
    public void audit(Long id, String remark, int state) {
        RealAuth realAuth = findById(id);

        if(realAuth!=null && realAuth.getState()==RealAuth.STATE_NORMAL){
            realAuth.setAuditor(UserContext.getLogininfo());
            realAuth.setAuditTime(new Date());
            realAuth.setState(state);
            Userinfo apply = userinfoService.findById(realAuth.getApplier().getId());
            if (state == RealAuth.STATE_AUDIT) {
                if(!apply.getIsRealAuth()){
                    apply.setBitState(BitStatesUtils.OP_REAL_AUTH);
                    apply.setRealName(realAuth.getRealName());
                    apply.setIdNumber(realAuth.getIdNumber());
                    apply.setRealAuthId(realAuth.getId());
                }
               ctx.publishEvent(new RealAuthSuccessEvent(this,realAuth));
            }else {
                apply.setRealAuthId(null);
            }
            userinfoService.update(apply);
            realAuthMapper.updateByPrimaryKey(realAuth);

        }
    }
}
